﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.AddIn.Hosting;
using Dashboard.Properties;
using System.IO;
using System.Collections.ObjectModel;

namespace Dashboard
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //AddInStore.UpdateAddIns(Settings.Default.PipelinePath);
                AddInStore.Update(Settings.Default.PipelinePath);
                Collection<AddInToken> addInTokens = AddInStore.FindAddIns(typeof(DashboardAddIn), Settings.Default.PipelinePath);

                //AddInProcess process = new AddInProcess();
                //process.KeepAlive = false;
                AppDomain newDomain=AppDomain.CreateDomain(addInTokens[0].AddInFullName);
                newDomain.UnhandledException+=new UnhandledExceptionEventHandler((o,args)=>{
                    //args.ExceptionObject
                });


                DashboardAddIn addIn = addInTokens[0].Activate<DashboardAddIn>(newDomain);
                //DashboardAddIn addIn=addInTokens[0].Activate<DashboardAddIn>( AddInSecurityLevel.Host);
                String abc=addIn.AddInId();
                FrameworkElement ele=addIn.Page();
                if(ele!=null)
                this.MainGrid.Children.Add(ele);
                System.Windows.Forms.UserControl  addIn1Form=addIn.WinFormPage();


                AppDomain newDomain2=AppDomain.CreateDomain(addInTokens[1].AddInFullName);
                newDomain2.UnhandledException+=new UnhandledExceptionEventHandler((o,args)=>{
                    //args.ExceptionObject
                });

                DashboardAddIn addIn2 = addInTokens[1].Activate<DashboardAddIn>(newDomain2);

                System.Windows.Forms.UserControl  addIn1Form2=addIn2.WinFormPage();
            }
            catch (DirectoryNotFoundException)
            {
                MessageBox.Show("Verify the pipeline directory in the config file");
                Application.Current.Shutdown();
            }
        }
    }
}
